<?php

//error_reporting(E_ALL);
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$mimeTypes = array(
    'Excel5' => array(
        'Content-type' => 'application/vnd.ms-excel',
        'extension' => 'xls',
    ),
    'Excel2007' => array(
        'Content-type' => 'application/vnd.ms-excel',
        'extension' => 'xlsx',
    ),
    'PDF' => array(
        'Content-type: application/pdf',
        'extension' => 'pdf',
    ),
    'HTML' => array(
        'Content-type' => 'text/html',
        'extension' => 'html',
    ),
    'CSV' => array(
        'Content-type' => 'application/csv',
        'extension' => 'csv',
    )
);
$attrData = array();
switch ($product_model) {
    case 'GOLD':
        $model = new ProductGold;
        break;
    case 'PL':
        $model = new ProductPowerleveling;
        break;
    case 'SKILL':
        $model = new ProductSkill;
        break;
    case 'SKILL_LEVEL':
        $model = new ProductSkillLevel;
        break;
    case 'GAME_SERVER':
        $model = new ProductCategory;
        break;
    default :
        $model = new ProductBase;
        $attrModel = new ProductAttribute;
        $game_info = ProductCategory::model()->findByPk($game);
        $cr = new CDbCriteria;
        $cr->compare('game_id', $game_info->parentid);
        $cr->compare('product_type', $product_model);
        $cr->compare('status', 1);
        $cr->order = 'order_sort ASC';
        foreach($attrModel->findAll($cr) as $value){
            $attrData[$value->id] = "{$value->name}_{$value->id}";
        }
        //throw new CHttpException(500, '产品模型不存在.');
        //exit;
        break;
}
$iniData = $model->attributeLabels();
if(!empty($attrData))
    $iniData += $attrData;

//导入接口
spl_autoload_unregister(array('YiiBase', 'autoload'));
Yii::import('application.extensions.phpexcel.Classes.PHPExcel', true);
$objPHPExcel = new PHPExcel();
spl_autoload_register(array('YiiBase', 'autoload'));
// Creating a workbook
$product_name = $product_model!='GAME_SERVER' ? Yii::app()->controller->model_array[$product_model]:'服务区';
$game_name = ProductCategory::model()->getGameNameById((int)$game);
$title = $game_name . '-' . $product_name . "-数据模版-" . date('Y-m-d');
$description = "数据中心产品导入模板";
$objPHPExcel->getProperties()->setCreator('Usfine');
$objPHPExcel->getProperties()->setTitle($title);
$objPHPExcel->getProperties()->setSubject('数据中心');
$objPHPExcel->getProperties()->setDescription($description);
$objPHPExcel->getProperties()->setCategory('');

//获取栏位标题
$i = 0;
foreach ($iniData as $key => $value) {
    if ($key == 'site_key' || $key == 'id' || $key == 'p_type' || $key=='update_time' || $key=='p_time') {//分站，自动编号，产品类型不入表格
        continue;
    }
    $fName = $value;
    
    if ($i <= 25) {
        $col = chr(ord('A') + $i);
    } else {
        $col = chr(ord('A')) . chr(ord('A') + $i - 26);
    }
    $colTitle = $col . "1";
    //echo $colTitle . "<br>";
    $head = $fName;
    $objPHPExcel->getActiveSheet()->setCellValue($colTitle, $head);
    $colValue = $col . "2";
    $objPHPExcel->getActiveSheet()->getColumnDimension($colValue)->setAutoSize(false);
    ++$i;
}

//输出表格
$exportType = "Excel2007";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $exportType);
ob_end_clean();
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-type: ' . $mimeTypes[$exportType]['Content-type']);
header('Content-Disposition: attachment; filename="' . $title . '.' . $mimeTypes[$exportType]['extension'] . '"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
Yii::app()->end();